<?php
/**
 * @version 0.8.3 2010-10-07
 * @package Joomla
 * @subpackage LeagueResults
 * @copyright (C)  2008, 2009, 2010 Henryk Drozd
 * @license GNU/GPL, see LICENSE.php
 * LeagueResults is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 2
 * as published by the Free Software Foundation.

 * LeagueResults is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

// Written on a template of Jombackup great plugin. Hats off!


defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.view');
jimport('joomla.filesystem.file');

class lrViewLr_backup extends JView {
	var $_backupfile;
	function display($tpl = null)
	{
		global $mainframe;
		
		$this->backup_now();
		
		parent::display($tpl);
	}
	
	function backup_now() 
	{
	global $mainframe;

	$CONFIG = new JConfig();
	$database 		= &JFactory::getDBO();
	$lr_abspath		= substr($CONFIG->tmp_path,0,-4);
	$lr_host		= $mainframe->getCfg( 'host' );
	$lr_user		= $mainframe->getCfg( 'user' );
	$lr_password	= $mainframe->getCfg( 'password' );
	$lr_db			= $mainframe->getCfg( 'db' );
	$lr_mailfrom	= $mainframe->getCfg( 'mailfrom' );
	$lr_fromname	= $mainframe->getCfg( 'fromname' );
	$lr_livesite	= $mainframe->getCfg( 'live_site' );
	$testing		= false;
	// You can manually set the production flag here if you don't want the "testing" option to kick in
	// at any point. Effectively it means that the mambot query will not be run until $okToContinue is true, which
	// only occurs if today's checkFile doesn't exist.
	// If you DO manually set this flag, then of course none of the testing data will be echoed to your browser
	
	$mediaPath		= $lr_abspath.DS.'media';
	$checkfileName	= 'lr_checkfile_';
	$today 			= date("Y-m-d");
	$dateCheckFile	= $checkfileName.$today;
	$okToContinue	= true;
	
	if (is_writable($mediaPath) )  // a couple of simple checks to see if we need to actually do anything
		{
			if (!$testing)
				{
				if (!touch($mediaPath.DS.$dateCheckFile)) // Oops, we can't create the date check file, no point in continuing otherwise this plugin will run EVERY time a link is clicked in Joomla. Not good.
					{
					echo "Couldn't create check file. Please ensure that $mediaPath is writable by the web server";
					$okToContinue=false;
					}
				}
		}
	else
		$okToContinue=false;
		
	if ($testing)
		{
		if ($okToContinue) 
			echo "Backing up and emailing system mysqldata<br />";
		else
			echo "Not backing up system data<br />";
		}
	if ($okToContinue) 
		{
		// No need to do the require beforehand if not ok to continue, so we'll do it here to save an eeny weeny amount of time

		require_once (JPATH_COMPONENT_ADMINISTRATOR.DS.'assets'.DS.'mysql_db_backup.class.php');

		JFile::delete($mediaPath.DS.$dateCheckFile);
		
		
		$deletefile		= false;
		$compress		= 1;
		$backuppath		= 0;
		$verbose		= 1;
		
		// Ok, let's crack on. First we want to get rid of yesterday's jombackup_checkfile, no need to have that lying around now
		// Now we need to create the backup
		$backup_obj 	= new lr_MySQL_DB_Backup();
		$dp = & $database->_table_prefix;
		$backup_obj->tablesToInclude = array(
				$dp.'lr_clubs',
				$dp.'lr_leagueseasons',
				$dp.'lr_players',
				$dp.'lr_settings',
				$dp.'lr_tablecontents',
				$dp.'lr_tc_homeaway',
				$dp.'lr_tableheaders',
				$dp.'lr_teamplayers',
				$dp.'lr_teams',
				$dp.'lr_fixtures',
				$dp.'lr_tournaments',
				$dp.'lr_tournament_players',
				$dp.'lr_stats_measures',
				$dp.'lr_stats_sets',
				$dp.'lr_stats_setelements',
				$dp.'lr_stats_values',
				$dp.'lr_roles',
				$dp.'lr_pointslogic',
				);


		
		
		$result			= $this->lrBackup($backup_obj,$lr_host,$lr_user,$lr_password,$lr_db,$pluginParams,$mediaPath,$lr_fromname,$compress,$backuppath);
		$this->_backupfile = $backup_obj->lr_file_name;
		
		$backupFile		= $backup_obj->lr_file_name;

		if ($deletefile=="1" && !empty($backupFile) )
			{
			if ($testing)
				echo "Deleting backup file $backupFile";
			unlink($backupFile);	
			}
		else if ($testing)
			echo "Not deleting backup file $backup_obj->jombackup_file_name";
		// Job done
		}
	}
	
	function lrBackup(&$backup_obj,$lr_host,$lr_user,$lr_password,$lr_db,$pluginParams,$mediaPath,$lr_fromname,$compress,$backuppath)
	{
	$Body 				= 'Mysql backup from'.$lr_fromname;
	$drop_tables 		= 0;
	$create_tables 		= 0;
	$struct_only 		= 0;
	$locks 				= 1;
	$comments 			= 1;

	
	// Let's set the tables to ignore array.
		
	if (!empty($backuppath) && is_dir($backuppath) && @is_writable($backuppath)  )
		$backup_dir 		= $backuppath;
	else
		$backup_dir 		= $mediaPath;

	//----------------------- EDIT - REQUIRED SETUP VARIABLES -----------------------	
	$backup_obj->server 	= $lr_host;
	$backup_obj->port 		= 3306;
	$backup_obj->username 	= $lr_user;
	$backup_obj->password 	= $lr_password;
	$backup_obj->database 	= $lr_db;		
	//Tables you wish to backup. All tables in the database will be backed up if this array is null.
	$backup_obj->tables = array();
	//------------------------ END - REQUIRED SETUP VARIABLES -----------------------
	
	//-------------------- OPTIONAL PREFERENCE VARIABLES ---------------------
	//Add DROP TABLE IF EXISTS queries before CREATE TABLE in backup file.
	$backup_obj->drop_tables 	= $drop_tables;	
	//No table structure will be backed up if false
	$backup_obj->create_tables 	= $create_tables;
	//Only structure of the tables will be backed up if true.
	$backup_obj->struct_only 	= $struct_only;
	//Add LOCK TABLES before data backup and UNLOCK TABLES after
	$backup_obj->locks 			= $locks;
	//Include comments in backup file if true.
	$backup_obj->comments 		= $comments;
	//Directory on the server where the backup file will be placed. Used only if task parameter equals MSX_SAVE.
	$backup_obj->backup_dir 	= $backup_dir.DS;
	//Default file name format.
	$backup_obj->fname_format 	= 'd_m_Y__H_i_s';
	//Values you want to be intrerpreted as NULL
	$backup_obj->null_values 	= array( );
	
	$savetask = MSX_SAVE;		
	//Optional name of backup file if using 'MSX_APPEND', 'MSX_SAVE' or 'MSX_DOWNLOAD'. If nothing is passed, the default file name format will be used.
	$filename = '';
	//--------------------- END - REQUIRED EXECUTE VARIABLES ----------------------
	$result_bk = $backup_obj->Execute($savetask, $filename, $compress);
	if (!$result_bk)
		{
		$output = $backup_obj->error;
		}
	else
		{
		$output = $Body.': ' . strftime('%A %d %B %Y  - %T ') . ' ';
		}
	return array('result'=>$result_bk,'output'=>$output);
	}
	

}
?>